package com.songlanyun.modules.account.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.entity.AccountEntity;
import com.songlanyun.modules.account.entity.UserInfoEntity;
import com.songlanyun.modules.account.entity.UserVerifiedEntity;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.account.service.UserVerifiedService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员的信息表（如昵称，头像等）
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-06 14:07:42
 */
@RestController
@RequestMapping("admin/account/userinfo")
public class UserInfoAdminController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private UserVerifiedService userVerifiedService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("account:yxtuserinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("account:yxtuserinfo:info")
    public R info(@PathVariable("id") Long id){
        UserInfoEntity userInfo = userInfoService.getById(id);

        return R.ok().put("data", userInfo);
    }

    /**
     * 信息
     */
    @GetMapping("/byMobile/{mobile}")
    //@RequiresPermissions("account:yxtuserinfo:info")
    public R info(@PathVariable("mobile") String mobile){
        AccountEntity account = accountService.getByMobile(mobile);
        if(ObjectUtil.isNull(account))
        {
            throw new RRException("没有找到该手机号对应的用户");
        }
        String realName = "";
        UserVerifiedEntity optionUserVerifiedEntity = userVerifiedService.loadById(account.getId(), false);
        if(ObjectUtil.isNull(optionUserVerifiedEntity))
        {
            UserInfoEntity userInfoEntity = userInfoService.loadById(account.getId(), true);
            if(ObjectUtil.isNotNull(userInfoEntity))
            {
                realName = StringUtils.isNotEmpty(userInfoEntity.getRealName())?userInfoEntity.getNickname():userInfoEntity.getRealName();
            }
        }
        else
        {
            realName = optionUserVerifiedEntity.getRealName();
        }

        return R.ok().put("data", realName);
    }

    /**
     * 修改
     */
    @SysLog("修改会员信息")
    @PutMapping("/update")
    @RequiresPermissions("account:yxtuserinfo:update")
    public R update(@RequestBody UserInfoEntity userInfo){
        ValidatorUtils.validateEntity(userInfo);
        userInfoService.updateById(userInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除会员信息")
    @DeleteMapping("/delete")
    @RequiresPermissions("account:yxtuserinfo:delete")
    public R delete(@RequestBody Long[] ids){
        userInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
